package com.wiken.storeservice.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wiken.servicecommon.common.Code;
import com.wiken.servicecommon.common.R;
import com.wiken.servicecommon.common.exception.DefaultException;
import com.wiken.storeservice.DTO.GeographicLocation;
import com.wiken.storeservice.VO.StoreCategoriesVO;
import com.wiken.storeservice.entity.Store;
import com.wiken.storeservice.service.impl.StoreServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author wiken
 * @since 2022-12-28
 */
@RestController
@RequestMapping("/storeservice/store")
public class StoreController {
    @Autowired
    StoreServiceImpl storeService;

    @PostMapping("addStore")
    public R addStore(@RequestBody Store store, @RequestHeader("Authorization") String token){
        boolean success = storeService.addStore(store, token);

        if(!success) return R.error().code(Code.ERROR).message("添加店铺失败");

        return R.ok().code(Code.SUCCESS).message("添加店铺成功");
    }

    @PutMapping("updateStore")
    public R updateStore(@RequestBody Store store, @RequestHeader("Authorization") String token){
        boolean success = storeService.updateStore(store, token);

        if(!success) return R.error().code(Code.ERROR).message("更新店铺失败");

        return R.ok().code(Code.SUCCESS).message("更新店铺成功");
    }

    @DeleteMapping("delStore")
    public R delStore(@RequestBody Store store, @RequestHeader("Authorization") String token){
        boolean success = storeService.deleteStore(store, token);

        if(!success) return R.error().code(Code.ERROR).message("删除店铺失败");

        return R.ok().code(Code.SUCCESS).message("删除店铺成功");
    }

    @PostMapping("getStore/{storeId}")
    public R getStore(@PathVariable("storeId") String storeId){
        StoreCategoriesVO store = storeService.getStoreById(storeId);

        return R.ok().code(Code.SUCCESS).message("获取店铺成功").data("store", store);
    }

    @PostMapping("getStores")
    public R getStoreListByUserId(Store store, @RequestHeader("Authorization") String token){
        List<Store> storeList = storeService.getStoreByUserId(store, token);

        return R.ok().code(Code.SUCCESS).message("获取店铺成功").data("storeList", storeList);
    }

    @PostMapping("getStoreList")
    public R getStoreList(@RequestBody Page page,@RequestHeader("Authorization") String token){
        Page<Store> storeList = storeService.getStoreList(page, token);

        return R.ok().code(Code.SUCCESS).message("获取店铺成功").data("storeList", storeList);
    }

    @PostMapping("getStoreOfNearby")
    public R getStoreOfNearby(@RequestBody GeographicLocation geographicLocation){
        List<Store> storeList = storeService.getStoreOfNearby(geographicLocation);

        return R.ok().code(Code.SUCCESS).message("获取店铺成功").data("storeList", storeList);

    }

    @PostMapping("/getStoreOfNearbyByTypeId/{typeId}")
    public R getStoreOfNearbyByTypeId(@PathVariable("typeId") String typeId, @RequestBody GeographicLocation geographicLocation){
        List<Store> storeList = storeService.getStoreOfNearbyByCategory(typeId, geographicLocation);

        return R.ok().code(Code.SUCCESS).message("获取店铺成功").data("storeList", storeList);
    }
}
